import { activitiesCreate, activitiesUpdate } from "@/api/SecondmentDictionary";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { convertToTimestamp } from "@/utils/util";
import { message } from "antd";
import { useState } from "react";
import { useTranslation } from "react-i18next";
let getActivities: Function;
let actvtId: string;
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const formSchema: FormSchema[] = [
		{
			label: t("kevin_ui.activityName"),
			field: "actvtName", //活动名称
			component: "Input"
		},
		{
			label: t("kevin_ui.subject"),
			field: "actvtSubject", //活动主题
			component: "Input"
		},
		{
			label: t("kevin_ui.activityForm"),
			field: "actvtForm", //活动形式:
			component: "Select",
			componentProps: {
				options: [
					{ label: t("secondmentDictionary.onLine"), value: "1" },
					{ label: t("secondmentDictionary.offline"), value: "2" }
				]
			}
		},
		{
			label: t("viewComponents.patientInformation.validFrom"),
			field: "actvtStartDate", //活动起始日期
			component: "DatePicker"
		},
		{
			label: t("viewComponents.patientInformation.validTo"),
			field: "actvtEndDate", //活动终止日期
			component: "DatePicker"
		},
		{
			label: t("kevin_ui.activityPeriod"),
			field: "actvtExtraTime", //活动时段补充(上午/下午/晚上)
			component: "Select",
			componentProps: {
				options: [
					{ label: "8:00——12:00", value: "8:00——12:00" },
					{ label: "14:00——18:00", value: "14:00——18:00" },
					{ label: "20:00——22:00", value: "20:00——22:00" }
				]
			}
		},
		{
			label: t("kevin_ui.activityRules"),
			field: "actvtRegulations", //	活动规则信息
			component: "InputTextArea"
		},
		{
			label: t("kevin_ui.organizer"),
			field: "ownerGroupId", //主办单位
			component: "Input"
		},
		{
			label: t("kevin_ui.coOrganizer"),
			field: "coOrgGroupId", //协办单位
			component: "Input"
		}
	];
	const [isAdd, setIsAdd] = useState(true);
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		getActivities = data.getActivities;
		resetFields();
		if (data.record) {
			actvtId = data.record.actvtId;
			setFieldsValue(data.record);
			setIsAdd(false);
		}
	});

	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 180,
		baseColProps: {
			span: 24
		}
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		let api = activitiesCreate;
		console.log(formValue);
		if (!isAdd) {
			api = activitiesUpdate;
		}
		formValue.actvtStartDate = convertToTimestamp(formValue.actvtStartDate, true);
		formValue.actvtEndDate = convertToTimestamp(formValue.actvtEndDate, true);
		const res = await api({ ...formValue, actvtId });
		if (res) {
			actvtId ? message.success(t("kevin_ui.addedSuccessfully")) : message.success(t("kevin_ui.updateSuccess"));
			getActivities();
			closeDrawer();
		}
	};
	return (
		<BasicDrawer showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
